using System.Security.Cryptography;
using System.Text;

namespace Acme.MeiTuan.SDK.Tools;

public static class Sign
{
    public static string MD5Sign(string predata)
    {
        using var md5Hash = MD5.Create();
        var data = md5Hash.ComputeHash(Encoding.UTF8.GetBytes(predata));
        var sBuilder = new StringBuilder();
        for (var i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }

        return sBuilder.ToString();
    }

    public static string GetSingStr(object obj, string[] ExcludeKeys)
    {
        var dics = obj.ToDictionary<object>();

        foreach (var Key in ExcludeKeys)
        {
            dics.Remove(Key);
        }

        return dics.SortParamsByAscll();
    }

    public static string GetSingStr(List<KeyValuePair<string, string>> keyValuePairs)
    {
        var keyValues = keyValuePairs.ToDictionary(x => x.Key, x => x.Value);
        return SortParamsByAscll(keyValues);
    }

    //public static string GetSingStr(List<KeyValuePair<string, string>> keyValuePairs, string[] ExcludeKeys)
    //{
    //    var keyValues = keyValuePairs.ToDictionary(x => x.Key, x => x.Value);
    //    foreach (var item in ExcludeKeys)
    //    {
    //        keyValues.Remove(item);
    //    }
    //    return SortParamsByAscll(keyValues);
    //}

    private static string SortParamsByAscll(this IDictionary<string, object> paramsMap)
    {
        var str = new StringBuilder();
        var arrKeys = paramsMap.Keys.ToArray();
        Array.Sort(arrKeys, string.CompareOrdinal);
        foreach (var key in arrKeys)
        {
            str.Append(key + "=" + paramsMap[key] + "&");
        }

        return str.ToString().TrimEnd('&');
    }

    private static string SortParamsByAscll(this IDictionary<string, string> paramsMap)
    {
        var str = new StringBuilder();
        var arrKeys = paramsMap.Keys.ToArray();
        Array.Sort(arrKeys, string.CompareOrdinal);
        foreach (var key in arrKeys)
        {
            str.Append(key + "=" + paramsMap[key] + "&");
        }

        return str.ToString().TrimEnd('&');
    }

    /// <summary>
    ///     SHA1加密
    /// </summary>
    /// <param name="content">待加密的字符串</param>
    /// <param name="encode">编码方式</param>
    /// <returns></returns>
    public static string Sha1Sign(string content, Encoding encode)
    {
        try
        {
            SHA1 sha1 = new SHA1CryptoServiceProvider();
            var bytesIn = encode.GetBytes(content);
            var bytesOut = sha1.ComputeHash(bytesIn);
            sha1.Dispose();
            var result = BitConverter.ToString(bytesOut);
            result = result.Replace("-", "").ToLower();
            return result;
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

    /// <summary>
    ///     Sha256
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    public static string Sha256(string data)
    {
        var bytes = Encoding.UTF8.GetBytes(data);
        var hash = SHA256.Create().ComputeHash(bytes);

        var builder = new StringBuilder();
        foreach (var t in hash)
        {
            builder.Append(t.ToString("X2"));
        }

        return builder.ToString().ToLower();
    }
}